﻿using Microsoft.AspNetCore.Mvc;
using System;
using System.Collections.Generic;
using System.Text.RegularExpressions;
using System.Linq;

namespace Topro.Admin.Controllers
{
    using System.Net.Http;
    using Topro.Application.Interface;
    using Topro.Entity;
    using Topro.Entity.Enum;
    using Topro.Entity.Query;
    using Topro.Entity.Request.Dictionary;
    using Topro.Entity.Response.Dictionary;

    /// <summary>
    /// 字典控制器
    /// </summary>
    public class DictionaryController : BaseController
    {
        #region 注入
        /// <summary>
        /// 配置接口
        /// </summary>
        private readonly DictionaryService _dictionaryService;

        /// <summary>
        /// 
        /// </summary>
        /// <param name="settingsService">配置接口</param>
        public DictionaryController(DictionaryService dictionaryService)
        {
            _dictionaryService = dictionaryService;
        }
        #endregion

        /// <summary>
        /// 配置列表
        /// </summary>
        /// <param name="type"></param>
        /// <returns></returns>
        [AuthFilter(AuthCode.Dictionary0001)]
        public IActionResult Index(int type)
        {
            //类型
            ViewBag.Type = type;

            return View();
        }

        /// <summary>
        /// 查询
        /// </summary>
        /// <param name="query">查询条件</param>
        /// <returns>PagedList<DictionaryQueryResponse></returns>
        [AuthFilter(AuthCode.Dictionary0001)]
        public IActionResult DictionaryQuery(DictionaryQuery query)
        {
            var list = _dictionaryService.GetPager(query);

            return PartialView("_Query", list);
        }

        /// <summary>
        /// 新增、编辑
        /// </summary>
        /// <param name="id">编号</param>
        /// <param name="type">类型</param>
        /// <returns></returns>
        [AuthFilter(AuthCode.Dictionary0002, AuthCode.Dictionary0003)]
        public IActionResult DictionaryAddOrEdit(int? id,int type)
        {
            var model = new DictionaryResponse();
            if (id.HasValue)
            {
                model = _dictionaryService.Get(id.Value);
            }
            else
            {
                model.Type = type;
            }

            return View("Edit", model);
        }

        /// <summary>
        /// 新增、编辑
        /// </summary>
        /// <returns></returns>
        [AuthFilter(AuthCode.Dictionary0002, AuthCode.Dictionary0003)]
        public JsonResult SaveDictionary(DictionaryReqeust request)
        {
            var response = new JResult()
            {
                Success = false
            };

            try
            {
                #region 验证
                //类型
                if (request.Type<=0)
                {
                    throw new Exception("键不能为空.");
                }
                //键
                if (string.IsNullOrEmpty(request.Name))
                {
                    throw new Exception("键不能为空.");
                }

                var nameReg = new Regex(@"^.{1,40}$");
                if (!nameReg.IsMatch(request.Name))
                {
                    throw new Exception("键必须介于 1 和 40 之间");
                }
                //值
                if (string.IsNullOrEmpty(request.Value))
                {
                    throw new Exception("值不能为空.");
                }

                var codeReg = new Regex(@"^.{1,40}$");
                if (!codeReg.IsMatch(request.Value))
                {
                    throw new Exception("值必须介于 1 和 40 之间");
                }
                //排序
                if (string.IsNullOrEmpty(request.DisplayOrder))
                {
                    throw new Exception("排序不能为空.");
                }

                var sortNoReg = new Regex(@"^(([1-9]\d{0,1})|0)?$");
                if (!sortNoReg.IsMatch(request.DisplayOrder))
                {
                    throw new Exception("排序只能输入两位正整数.");
                }
                //状态
                if (!request.Status.HasValue)
                {
                    throw new Exception("选择状态.");
                }

                var status = new List<int>()
                {
                    (int)SettingsStatus.启用,
                    (int)SettingsStatus.禁用
                };

                if (!status.Contains(request.Status.Value))
                {
                    throw new Exception("状态参数错误.");
                }
                #endregion

                request.Ip = Ip;
                request.UserId = UserId;

                response = _dictionaryService.SaveDictionary(request);
                if (response.Success)
                {
                    try
                    {
                        using (var client = new HttpClient())
                        {
                            var res = client.GetAsync("http://10.90.20.99/Home/cache");
                        }
                    }
                    catch(Exception ex)
                    {
                    }
                }
            }
            catch (Exception ex)
            {
                response.Message = ex.Message;
            }


            return Json(response);
        }

        /// <summary>
        /// 更新状态
        /// </summary>
        /// <param name="id">编号</param>
        /// <param name="status">状态</param>
        /// <returns></returns>
        [AuthFilter(AuthCode.Dictionary0004)]
        [HttpPost]
        public JsonResult UpdateStatus(int id, sbyte status)
        {
            var response = new JResult()
            {
                Success = false
            };

            response = _dictionaryService.Updatestatus(id, status);

            return Json(response);
        }
    }
}
